package cn.cau.control;


import cn.cau.entity.CodeManageRecord;
import cn.cau.entity.CodeRule;
import cn.cau.entity.Options;
import cn.cau.service.CodeRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class CodeGenerateIndexController {
    @RequestMapping(value = "/codeGenerateIndex", method = RequestMethod.GET)
    public String codeGenerateIndex() {
        return "/codeGenerate/codeGenerateIndex";
    }
    @Autowired
    private CodeRuleService codeRuleService;

    @RequestMapping(value="/inputKey3",method=RequestMethod.POST)
    public ModelAndView inputKey3(@RequestParam(value = "rule_word")String rule_word) {
        List<CodeRule> c1 = codeRuleService.findCodeRuleByName(rule_word);
        String errorString="";
        String test1=null;
        test1=rule_word;
        //所选择规则不唯一，请重新查询规则
        if(c1.size()!=1){
            if(test1==null||test1=="")
            {
                errorString="未输入规则查询标识，请输入查询标识";
                ModelAndView modelAndView = new ModelAndView();
                modelAndView.addObject("error",errorString);
                modelAndView.setViewName("/codeGenerate/codeGenerateIndex");
                return modelAndView;
            }
            else {
                if(c1.size()==0)
                {
                    errorString = "不存在搜索的规则，请输入查询标识";
                    ModelAndView modelAndView = new ModelAndView();
                    modelAndView.addObject("error", errorString);
                    modelAndView.setViewName("/codeGenerate/codeGenerateIndex");
                    return modelAndView;
                }
                else
                {
                    errorString = "所选择规则不唯一，请重新查询规则";
                    ModelAndView modelAndView = new ModelAndView();
                    modelAndView.addObject("error", errorString);
                    modelAndView.setViewName("/codeGenerate/codeGenerateIndex");
                    return modelAndView;
                }
            }
        }
        else
        {
                CodeRule c2 = c1.get(0);
                if(c2.getCodeState()!=2){
                    errorString = "所选择规则状态不可用，请重新查询规则";
                    ModelAndView modelAndView = new ModelAndView();
                    modelAndView.addObject("error", errorString);
                    modelAndView.setViewName("/codeGenerate/codeGenerateIndex");
                    return modelAndView;
                }
                else
                {
                    String rule_show = c2.toString();
                    int rule_id = c2.getCodeRuleId();
                    ModelAndView modelAndView = new ModelAndView();
                    modelAndView.addObject("rule_word_result", rule_word);
                    modelAndView.addObject("coderuleid", rule_id);
                    modelAndView.addObject("error",errorString);
                    modelAndView.addObject("rule",c2);
                    modelAndView.addObject("coderuleshow", rule_show);
                    modelAndView.setViewName("/codeGenerate/codeGenerateIndex");
                    return modelAndView;
                }
    }
    }

}
